package com.ruoyi.yuanxuan.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yuanxuan.domain.Yuanxuan;
import com.ruoyi.yuanxuan.service.IYuanxuanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * yuanxuanController
 *
 * @author ruoyi
 * @date 2025-07-02
 */
@RestController
@RequestMapping("/yuanxuan")
public class YuanxuanController extends BaseController
{
    @Autowired
    private IYuanxuanService yuanxuanService;

    /**
     * 查询是否过期
     */
    @GetMapping("/getKey")
    public ResponseEntity<String> getKey(String key, String mac){
        Yuanxuan yuanxuan = new Yuanxuan();
        yuanxuan.setKey(key);
        yuanxuan.setEndTime(DateUtils.getNowDate());
        List<Yuanxuan> key1 = yuanxuanService.getKey(yuanxuan);
        if (key1.size() > 0){
            if (StringUtils.isEmpty(key1.get(0).getMac())){
                yuanxuan = new Yuanxuan();
                yuanxuan.setId(key1.get(0).getId());
                yuanxuan.setMac(mac);
                yuanxuanService.updateYuanxuan(yuanxuan);
                return ResponseEntity.status(HttpStatus.OK).body("success"); // 404;
            }else if (mac.equals(key1.get(0).getMac())){
                return ResponseEntity.status(HttpStatus.OK).body("success"); // 404;
            }
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"); // 404
    }

    /**
     * 插入mac
     */
    @GetMapping("/setMac")
    public AjaxResult setMac(String mac,String key){
        Yuanxuan yuanxuan = new Yuanxuan();
        List<Yuanxuan> key1 = yuanxuanService.getKey(yuanxuan);
        if (StringUtils.isEmpty(key1.get(0).getMac())){
            yuanxuan.setKey(key);
            yuanxuan.setMac(mac);
            yuanxuanService.insertYuanxuan(yuanxuan);
            return success();
        }else if (mac.equals(key1.get(0).getMac())){
            return success();
        }

        return error();
    }


    /**
     * 查询yuanxuan列表
     */
    @PreAuthorize("@ss.hasPermi('yuanxuan:yuanxuan:list')")
    @GetMapping("/list")
    public TableDataInfo list(Yuanxuan yuanxuan)
    {
        startPage();
        List<Yuanxuan> list = yuanxuanService.selectYuanxuanList(yuanxuan);
        return getDataTable(list);
    }

    /**
     * 导出yuanxuan列表
     */
    @PreAuthorize("@ss.hasPermi('yuanxuan:yuanxuan:export')")
    @Log(title = "yuanxuan", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Yuanxuan yuanxuan)
    {
        List<Yuanxuan> list = yuanxuanService.selectYuanxuanList(yuanxuan);
        ExcelUtil<Yuanxuan> util = new ExcelUtil<Yuanxuan>(Yuanxuan.class);
        util.exportExcel(response, list, "yuanxuan数据");
    }

    /**
     * 获取yuanxuan详细信息
     */
    @PreAuthorize("@ss.hasPermi('yuanxuan:yuanxuan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(yuanxuanService.selectYuanxuanById(id));
    }

    /**
     * 新增yuanxuan
     */
    @PreAuthorize("@ss.hasPermi('yuanxuan:yuanxuan:add')")
    @Log(title = "yuanxuan", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Yuanxuan yuanxuan)
    {
        return toAjax(yuanxuanService.insertYuanxuan(yuanxuan));
    }

    /**
     * 修改yuanxuan
     */
    @PreAuthorize("@ss.hasPermi('yuanxuan:yuanxuan:edit')")
    @Log(title = "yuanxuan", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Yuanxuan yuanxuan)
    {
        return toAjax(yuanxuanService.updateYuanxuan(yuanxuan));
    }

    /**
     * 删除yuanxuan
     */
    @PreAuthorize("@ss.hasPermi('yuanxuan:yuanxuan:remove')")
    @Log(title = "yuanxuan", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(yuanxuanService.deleteYuanxuanByIds(ids));
    }
}
